home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / basename / RCS / basename.c,v < prev   
Encoding:
Text File  |  1988-12-13  |  2.2 KB  |  132 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     88.12.13.11.05.07;  author rab;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     88.11.28.14.05.10;  author rab;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @
  22. @
  23.  
  24.  
  25. 1.2
  26. log
  27. @rewrote basename to make it more readable, and provide
  28. better error msgs.  Also fixed a bug that occurs if the
  29. filename is shorter than the suffix.
  30. @
  31. text
  32. @/*
  33.  * basename.c --
  34.  *
  35.  *  Basename deletes any prefix ending with '/' from its first
  36.  *  argument, and also deletes any suffix from the first argument
  37.  *  that matches the second argument.  The resulting string is
  38.  *  printed to the standard output.
  39.  *
  40.  * Copyright 1988 Regents of the University of California
  41.  * Permission to use, copy, modify, and distribute this
  42.  * software and its documentation for any purpose and without
  43.  * fee is hereby granted, provided that the above copyright
  44.  * notice appear in all copies.  The University of California
  45.  * makes no representations about the suitability of this
  46.  * software for any purpose.  It is provided "as is" without
  47.  * express or implied warranty.
  48.  */
  49.  
  50. #ifndef lint
  51. static char rcsid[] = "$Header$";
  52. #endif
  53.  
  54. #include <stdio.h>
  55. #include <string.h>
  56. #include <stdlib.h>
  57.  
  58. void
  59. main(argc, argv)
  60.     int argc;
  61.     char **argv;
  62. {
  63.     char *s;
  64.     int len;
  65.  
  66.     if (argc < 2) {
  67.     fputs("usage: basename string [suffix]\n", stderr);
  68.     exit(1);
  69.     }
  70.     if ((s = strrchr(argv[1], '/')) == NULL) {
  71.     s = argv[1];
  72.     } else {
  73.     ++s;
  74.     }
  75.     if (argc > 2) {
  76.     if ((len = strlen(s) - strlen(argv[2])) >= 0) {
  77.         if (strcmp(s + len, argv[2]) == 0)
  78.         s[len] = 0;
  79.     }
  80.     }
  81.     puts(s);
  82.     exit(0);
  83. }
  84.  
  85. @
  86.  
  87.  
  88. 1.1
  89. log
  90. @Initial revision
  91. @
  92. text
  93. @d1 17
  94. a17 1
  95. static char *sccsid = "@@(#)basename.c    4.2 (Berkeley) 10/20/82";
  96. d19 3
  97. a21 1
  98. #include    <stdio.h>
  99. d23 5
  100. d29 2
  101. a30 1
  102. char **argv;
  103. d32 2
  104. a33 1
  105.     register char *p1, *p2, *p3;
  106. d35 13
  107. a47 3
  108.     if (argc < 2) {
  109.         putchar('\n');
  110.         exit(1);
  111. d49 3
  112. a51 17
  113.     p1 = argv[1];
  114.     p2 = p1;
  115.     while (*p1) {
  116.         if (*p1++ == '/')
  117.             p2 = p1;
  118.     }
  119.     if (argc>2) {
  120.         for(p3=argv[2]; *p3; p3++) 
  121.             ;
  122.         while(p1>p2 && p3>argv[2])
  123.             if(*--p3 != *--p1)
  124.                 goto output;
  125.         *p1 = '\0';
  126.     }
  127. output:
  128.     puts(p2, stdout);
  129.     exit(0);
  130. d53 1
  131. @
  132.